﻿using Newtonsoft.Json;
using PanhuaWin.NCModel;
using PanhuaWin.WMS2NC888;
using PanhuaWin.工厂.仓库.下线;
using PanhuaWin.通用;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Windows.Forms;

namespace PanhuaWin.仓库.下线
{
    public partial class Form打印标签 : UIPage
    {
        //BarTender.Application ba = new BarTender.Application();
        BarTender.Application ba;
        BarTender.Format bfmt;
        int 开始行;
        int 结束行;
        int 开始列;
        int 结束列;


        private UIRadioButton 当前来源选项 = new UIRadioButton();
        private bool 是否已打印;
        public Form打印标签()
        {
            InitializeComponent();
        }

        private void Form打印标签_Load(object sender, EventArgs e)
        {
            ba = new BarTender.Application();
            bfmt = ba.Formats.Open(System.Windows.Forms.Application.StartupPath + "\\攀华大标签.btw");
            this.uiDatePicker开始日期.Value = DateTime.Today;
            this.uiDatePicker结束日期.Value = DateTime.Today;
            uiDataGridView产出.AutoGenerateColumns = false;
            pub.ListWorkShop(pub.sys_user, this.uiComboBoxWorkShop);
            ListMaterial();
        }


        private void uiButton查询_Click(object sender, EventArgs e)
        {
            listData();
        }

        private void listData()
        {
            if (uiRadioButton酸洗.Checked)
            {
                queryLablePrint("酸洗");
            }

            if (uiRadioButton轧硬.Checked)
            {
                queryLablePrint("轧硬");
            }
            if (uiRadioButton镀锌.Checked)
            {
                queryLablePrint("镀锌");
            }
            if (uiRadioButton彩涂.Checked)
            {
                queryLablePrint("彩涂");
            }
            if (uiRadioButton罩退.Checked)
            {
                queryLablePrint("罩退");
            }
            if (uiRadioButton退火.Checked)
            {
                queryLablePrint("退火");
            }
            if (uiRadioButton硅钢.Checked)
            {
                queryLablePrint("硅钢");
            }
        }

        private void queryLablePrint(string 来源种类)
        {
            decimal 总重量;
            decimal 总长度;
            int 总卷数;
            总重量 = 0;
            总长度 = 0;
            string sql = @"select  [来源种类], [来源产线], [来源班组], [工厂编号], [生产批号], [存货编号], [品名], [钢号], [条码], [厚度], 
                            [产地], [长度], [规格], [等级], 
                            [表面状态], [表面处理], [净重], [备注], [生产日期], 仓库, 货位, 锌层,包装形式 ,客户,是否已打印, 打印次数,品名,颜色,max(操作时间) 操作时间
                            from Bus_LablePrint ";

            if (来源种类 == "镀锌")
            {
                sql += " where (来源种类 like '%镀锌%' or 来源种类 like '%彩基板%')and 1 = 1  ";
            }
            else
            {
                sql += " where 来源种类 like '%" + 来源种类 + "%' and 1 = 1  ";
            }

            if (!checkBox包含已打印.Checked)
            {
                sql += "and 生产批号 not in (select VolumeCodeNew from Bus_Volume union all select VolumeCodeNew from Bus_StockInVolume)";
            }
            sql += "and 生产日期 >= '" + this.uiDatePicker开始日期.Value + "' and 生产日期 < '" + this.uiDatePicker结束日期.Value.AddDays(1) + "'";

            if (!string.IsNullOrEmpty(uiTextBox规格型号1.Text))
            {
                sql += " and 规格 = '" + uiTextBox规格型号1.Text + "'";
            }
            if (!string.IsNullOrEmpty(uiComboBoxWorkShop.SelectedText))
            {
                sql += " and 工厂编号 = '" + uiComboBoxWorkShop.SelectedValue.ToString() + "'";
            }
            if (!string.IsNullOrEmpty(uiTextBox品名.Text))
            {
                sql += " and 品名 = '" + uiTextBox品名.Text + "'";
            }

            if (!string.IsNullOrEmpty(uiTextBox生产批号.Text))
            {
                sql += " and 生产批号 like '%" + uiTextBox生产批号.Text.Trim().ToUpper() + "%'";
            }
            if (!string.IsNullOrEmpty(uiComboBox材质.Text) && uiComboBox材质.Text != "请选择")
            {
                sql += " and 钢号 = '" + uiComboBox材质.Text + "'";
            }

            if (!string.IsNullOrEmpty(uiComboBox产线.Text) && uiComboBox产线.Text != "请选择")
            {
                sql += " and 来源产线 = '" + uiComboBox产线.Text + "'";
            }

            sql += " group by  [来源种类], [来源产线], [来源班组], [工厂编号], [生产批号], [存货编号], [品名], [钢号], [条码], [厚度], [产地], [长度], [规格], [等级], [表面状态], [表面处理], [净重], [备注], [生产日期], 仓库, 货位, 锌层,包装形式,客户,是否已打印, 打印次数,Bus_LablePrint.颜色 order by Bus_LablePrint.品名,规格, 生产批号 ";
            DataTable dt = pub.GetDataTable(pub.ConnectionString, sql);
            uiDataGridView产出.AutoGenerateColumns = false;
            uiDataGridView产出.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            uiDataGridView产出.DataSource = dt;
            uiDataGridView产出.Refresh();

            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    decimal.TryParse(dt.Rows[i]["净重"]?.ToString(), out decimal a);
                    decimal 净重 = a;
                    总重量 += 净重;
                    decimal.TryParse(dt.Rows[i]["长度"].ToString(), out decimal b);
                    总长度 += b;
                }
            }

            this.uiTextBox总卷数.Text = dt.Rows.Count.ToString();
            this.uiTextBox总重量.Text = Math.Round(总重量, 0).ToString();
            this.uiTextBox总米数.Text = 总长度.ToString();
        }

        private void uiRadioButton酸洗_CheckedChanged(object sender, EventArgs e)
        {
            ListWorkLine((sender as UIRadioButton).Text, this.uiComboBox产线);
        }

        private void ListWorkLine(string WorkType, UIComboBox uiComboBox产线)
        {
            string sql;
            sql = @"SELECT '' WorkLineCode,'请选择' WorkLineName
                    union all
                    SELECT   WorkLineCode, WorkLineName
                    FROM      Dic_WorkLine where WorkType='" + WorkType + "' order by WorkLineCode";
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            uiComboBox产线.DataSource = tb;
            uiComboBox产线.DisplayMember = "WorkLineName";
            uiComboBox产线.ValueMember = "WorkLineCode";
            uiComboBox产线.Refresh();
        }
        public void ListMaterial()
        {
            string sql;
            sql = @"  select '请选择' ProductMaterial
                    union all
                    select distinct ProductMaterial from Dic_WorkTypeMaterial";
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb.Rows.Count > 0)
            {
                uiComboBox材质.DataSource = tb;
                uiComboBox材质.DisplayMember = "ProductMaterial";
                uiComboBox材质.ValueMember = "ProductMaterial";
                uiComboBox材质.Refresh();
            }
            else
            {
                uiComboBox材质.DataSource = null;

                uiComboBox材质.Refresh();
            }
        }
        private void uiRadioButton轧硬_CheckedChanged(object sender, EventArgs e)
        {
            当前来源选项 = sender as UIRadioButton;
            ListWorkLine((sender as UIRadioButton).Text, this.uiComboBox产线);
        }

        private void uiRadioButton镀锌_CheckedChanged(object sender, EventArgs e)
        {
            当前来源选项 = sender as UIRadioButton;
            ListWorkLine((sender as UIRadioButton).Text, this.uiComboBox产线);
        }

        private void uiRadioButton彩涂_CheckedChanged(object sender, EventArgs e)
        {
            当前来源选项 = sender as UIRadioButton;
            ListWorkLine((sender as UIRadioButton).Text, this.uiComboBox产线);
        }

        private void uiRadioButton罩退_CheckedChanged(object sender, EventArgs e)
        {
            当前来源选项 = sender as UIRadioButton;
            ListWorkLine((sender as UIRadioButton).Text, this.uiComboBox产线);
        }

        private void uiRadioButton退火_CheckedChanged(object sender, EventArgs e)
        {
            当前来源选项 = sender as UIRadioButton;
            ListWorkLine((sender as UIRadioButton).Text, this.uiComboBox产线);
        }

        private void uiRadioButton硅钢_CheckedChanged(object sender, EventArgs e)
        {
            当前来源选项 = sender as UIRadioButton;
            ListWorkLine((sender as UIRadioButton).Text, this.uiComboBox产线);
        }

        private void uiDataGridView产出_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1 && this.uiDataGridView产出.Columns[e.ColumnIndex].Name == "C打印大标签")
            {
                //DataTable dt = uiDataGridView产出.DataSource as DataTable;
                //string 存货编号 = dt.Rows[e.RowIndex]["存货编号"].ToString();
                //string 生产批号 = dt.Rows[e.RowIndex]["生产批号"].ToString();
                //string 来源种类 = dt.Rows[e.RowIndex]["来源种类"].ToString();
                //string Customer = dt.Rows[e.RowIndex]["客户"].ToString();
                //string 品名 = dt.Rows[e.RowIndex]["品名"].ToString();
                //string 钢号 = dt.Rows[e.RowIndex]["钢号"].ToString();
                //string 规格 = dt.Rows[e.RowIndex]["规格"].ToString();
                打印(e.RowIndex);
                //if (dt.Rows[e.RowIndex]["存货编号"] != null && !string.IsNullOrEmpty(dt.Rows[e.RowIndex]["存货编号"].ToString().Trim()))
                //{

                //    string 工厂编号 = dt.Rows[e.RowIndex]["工厂编号"].ToString();
                //    //string 生产批号 = dt.Rows[e.RowIndex]["生产批号"].ToString();
                //    //string 品名 = dt.Rows[e.RowIndex]["品名"].ToString();
                //    //string 钢号 = dt.Rows[e.RowIndex]["钢号"].ToString();
                //    string 厚度 = dt.Rows[e.RowIndex]["厚度"].ToString();
                //    string 长度 = dt.Rows[e.RowIndex]["长度"].ToString();
                //    //string 规格 = dt.Rows[e.RowIndex]["规格"].ToString();
                //    string 等级 = dt.Rows[e.RowIndex]["等级"].ToString();
                //    string 表面状态 = dt.Rows[e.RowIndex]["表面状态"].ToString();
                //    string 表面处理 = dt.Rows[e.RowIndex]["表面处理"].ToString();
                //    string 净重 = dt.Rows[e.RowIndex]["净重"].ToString();
                //    string 备注 = dt.Rows[e.RowIndex]["备注"].ToString();
                //    string 生产日期 = dt.Rows[e.RowIndex]["生产日期"].ToString();
                //    string departCode = queryDeptCode(当前来源选项.Tag.ToString(), 生产批号);
                //    string 仓库 = dt.Rows[e.RowIndex]["仓库"].ToString();
                //    string 锌层 = dt.Rows[e.RowIndex]["锌层"].ToString();
                //    string 包装形式 = dt.Rows[e.RowIndex]["包装形式"].ToString();
                //    //string 产地 = dt.Rows[e.RowIndex]["产地"].ToString();
                //    //产地暂时用万达
                //    string 产地 = "万达";
                //    //塞入 Bus_Volume
                //  int res =  insertIntoBus_Volume(工厂编号, 生产批号, 存货编号, departCode, 品名, 钢号, 长度, 厚度,  规格, 等级, 表面状态, 表面处理, 净重, 备注, 生产日期, 产地, 仓库, 锌层, 包装形式);
                //    //生成入库单 检查这个批号有没有生成过
                //    string sqlqueryIsInstock = @"select * from Bus_StockInV
                //                                left join Bus_StockInVolume on Bus_StockInV.guid = Bus_StockInVolume.guid
                //                                where VolumeCodeNew =  '" + 生产批号 + "'";
                //    DataTable dtIsInstock = pub.GetDataTable(pub.ConnectionString, sqlqueryIsInstock);
                //    if (dtIsInstock != null && dtIsInstock.Rows.Count > 0)
                //    {
                //        int r = print(来源种类, 工厂编号, 生产批号, 品名, 钢号, 长度, 规格, 等级, 表面状态, 表面处理, 净重, 备注, 生产日期);
                //        if (dtIsInstock.Rows[0]["IsSendToNC"].ToString() == "N")
                //        {
                //            pub.sendToNC产成品(dtIsInstock.Rows[0]["DocCode"].ToString());
                //        }
                //    }
                //    else
                //    {
                //        int r = print(来源种类, 工厂编号, 生产批号, 品名, 钢号, 长度, 规格, 等级, 表面状态, 表面处理, 净重, 备注, 生产日期);
                //        string docCode = createBus_StockInV(工厂编号, 生产批号, 存货编号, departCode, 品名, 钢号, 长度, 厚度, 规格, 等级, 表面状态, 表面处理, 净重, 备注, 生产日期, 产地, 锌层, 包装形式, Customer, 仓库);
                //        pub.sendToNC产成品(docCode);
                //    }
                //}
                //else
                //{
                //    Form补录存货编码 form补录存货编码 = new Form补录存货编码();
                //    form补录存货编码.shortName = 品名;
                //    form补录存货编码.INVSPEC = 规格;
                //    form补录存货编码.INVTYPE = 钢号;
                //    if (form补录存货编码.ShowDialog(this) == DialogResult.OK)
                //    {
                //        if (!string.IsNullOrEmpty(form补录存货编码.存货编号))
                //        {
                //            string 存货号 = form补录存货编码.存货编号;
                //            //更新编码
                //            string sqlupdateLabelPrintInvCode = "update Bus_LablePrint set 存货编号 = '"+存货号 + "' where 生产批号 = '"+ 生产批号 + "'";
                //            pub.ExecuteSql(pub.ConnectionString, sqlupdateLabelPrintInvCode, "更新Bus_LablePrint表 生产批号：【" + 生产批号 + "】的存货编号【" + 存货编号 + "】");
                //            update产出InvCode(来源种类,生产批号, 存货号);
                //            listData();
                //        }
                //        else
                //        {
                //            MessageBox.Show("缺少存货编号，不能打印");
                //        }

                //    }
                //    else
                //    {
                //        MessageBox.Show("缺少存货编号，不能打印");
                //    }

                //}
            }
        }

        private void update产出InvCode(string 来源种类, string 生产批号, string 存货编号)
        {
            if (来源种类.Contains("镀锌"))
            {
                string updateBus_zincoutput = "upate bus_zincoutput set InventoryCode = '" + 存货编号 + "' where OutputVolumeNew = '" + 生产批号 + "'";
                pub.ExecuteSql(pub.ConnectionString, updateBus_zincoutput, "更新镀锌产出表OutputVolumeNew：【" + 生产批号 + "】的存货编号【" + 存货编号 + "】");
            }
            if (来源种类.Contains("酸洗"))
            {
                string updateBus_zincoutput = "upate Bus_PicklingOutput set InventoryCode = '" + 存货编号 + "' where OutputVolumeNew = '" + 生产批号 + "'";
                pub.ExecuteSql(pub.ConnectionString, updateBus_zincoutput, "更新酸洗产出表OutputVolumeNew：【" + 生产批号 + "】的存货编号【" + 存货编号 + "】");
            }
            if (来源种类.Contains("轧硬"))
            {
                string updateBus_zincoutput = "upate Bus_HardOutput set InventoryCode = '" + 存货编号 + "' where OutputVolumeNew = '" + 生产批号 + "'";
                pub.ExecuteSql(pub.ConnectionString, updateBus_zincoutput, "更新轧硬产出表OutputVolumeNew：【" + 生产批号 + "】的存货编号【" + 存货编号 + "】");
            }
            if (来源种类.Contains("彩涂"))
            {
                string updateBus_zincoutput = "upate Bus_PaintOutput set InventoryCode = '" + 存货编号 + "' where OutputVolumeNew = '" + 生产批号 + "'";
                pub.ExecuteSql(pub.ConnectionString, updateBus_zincoutput, "更新彩涂产出表OutputVolumeNew：【" + 生产批号 + "】的存货编号【" + 存货编号 + "】");
            }
            if (来源种类.Contains("罩退"))
            {
                string updateBus_zincoutput = "upate Bus_CoverFireOutput set InventoryCode = '" + 存货编号 + "' where OutputVolumeNew = '" + 生产批号 + "'";
                pub.ExecuteSql(pub.ConnectionString, updateBus_zincoutput, "更新罩退产出表OutputVolumeNew：【" + 生产批号 + "】的存货编号【" + 存货编号 + "】");
            }
            if (来源种类.Contains("退火"))
            {
                string updateBus_zincoutput = "upate Bus_FireOutput set InventoryCode = '" + 存货编号 + "' where OutputVolumeNew = '" + 生产批号 + "'";
                pub.ExecuteSql(pub.ConnectionString, updateBus_zincoutput, "更新退火产出表OutputVolumeNew：【" + 生产批号 + "】的存货编号【" + 存货编号 + "】");
            }
            if (来源种类.Contains("硅钢"))
            {
                string updateBus_zincoutput = "upate Bus_SiOutput set InventoryCode = '" + 存货编号 + "' where OutputVolumeNew = '" + 生产批号 + "'";
                pub.ExecuteSql(pub.ConnectionString, updateBus_zincoutput, "更新硅钢产出表OutputVolumeNew：【" + 生产批号 + "】的存货编号【" + 存货编号 + "】");
            }
        }

        private int print(string 来源种类, string 工厂编号, string 生产批号, string 品名, string 钢号, string 长度, string 规格, string 等级, string 表面状态, string 表面处理, string 净重, string 标准, string 生产日期)
        {
            bfmt.SetNamedSubStringValue("生产批号", "");
            bfmt.SetNamedSubStringValue("品名", "");
            bfmt.SetNamedSubStringValue("钢号", "");
            bfmt.SetNamedSubStringValue("长度", "");
            bfmt.SetNamedSubStringValue("规格", "");
            bfmt.SetNamedSubStringValue("等级", "");
            bfmt.SetNamedSubStringValue("表面状态", "");
            bfmt.SetNamedSubStringValue("表面处理", "");
            bfmt.SetNamedSubStringValue("净重", "");
            bfmt.SetNamedSubStringValue("备注", "");
            bfmt.SetNamedSubStringValue("生产日期", "");
            //string 生产批号 = dt.Rows[index]["生产批号"].ToString();
            //string 品名 = dt.Rows[index]["品名"].ToString();
            //string 钢号 = dt.Rows[index]["钢号"].ToString();
            //string 长度 = dt.Rows[index]["长度"].ToString();
            //string 规格 = dt.Rows[index]["规格"].ToString();
            //string 等级 = dt.Rows[index]["等级"].ToString();
            //string 表面状态 = dt.Rows[index]["表面状态"].ToString();
            //string 表面处理 = dt.Rows[index]["表面处理"].ToString();
            //string 净重 = dt.Rows[index]["净重"].ToString();
            //string 备注 = dt.Rows[index]["备注"].ToString();
            //string 生产日期 = dt.Rows[index]["生产日期"].ToString();

            //BarTender.Application ba = new BarTender.Application();
            //BarTender.Format bfmt;
            //bfmt = ba.Formats.Open(System.Windows.Forms.Application.StartupPath + "\\攀华大标签.btw");
            //bfmt.SetNamedSubStringValue("生产批号", dt.Rows[index]["生产批号"].ToString());
            //bfmt.SetNamedSubStringValue("品名", dt.Rows[index]["品名"].ToString());
            //bfmt.SetNamedSubStringValue("钢号", dt.Rows[index]["钢号"].ToString());
            //bfmt.SetNamedSubStringValue("长度", dt.Rows[index]["长度"].ToString());
            //bfmt.SetNamedSubStringValue("规格", dt.Rows[index]["规格"].ToString());
            //bfmt.SetNamedSubStringValue("等级", dt.Rows[index]["等级"].ToString());
            //bfmt.SetNamedSubStringValue("表面状态", dt.Rows[index]["表面状态"].ToString());
            //bfmt.SetNamedSubStringValue("表面处理", dt.Rows[index]["表面处理"].ToString());
            //bfmt.SetNamedSubStringValue("净重", dt.Rows[index]["净重"].ToString());
            //bfmt.SetNamedSubStringValue("备注", dt.Rows[index]["备注"].ToString());
            //bfmt.SetNamedSubStringValue("生产日期", dt.Rows[index]["生产日期"].ToString());
            //bfmt = ba.Formats.Open(System.Windows.Forms.Application.StartupPath + "\\攀华大标签.btw");
            if (表面状态.Trim() == "普通")
            {
                表面状态 = "";
            }
            bfmt.SetNamedSubStringValue("生产批号", 生产批号);
            bfmt.SetNamedSubStringValue("品名", 品名);
            bfmt.SetNamedSubStringValue("钢号", 钢号);
            bfmt.SetNamedSubStringValue("长度", 长度);
            bfmt.SetNamedSubStringValue("规格", 规格);
            bfmt.SetNamedSubStringValue("等级", 等级);
            bfmt.SetNamedSubStringValue("表面状态", 表面状态);
            bfmt.SetNamedSubStringValue("表面处理", 表面处理);
            bfmt.SetNamedSubStringValue("净重", 净重);
            bfmt.SetNamedSubStringValue("备注", 标准);
            bfmt.SetNamedSubStringValue("生产日期", 生产日期);
            //bfmt.PrintOut();
            //string sql = "insert into Bus_LablePrint(guid, 工厂编号, 生产批号,  品名, 钢号, 条码, 长度, 规格, 等级, 表面状态,  ";
            //sql += " 表面处理, 净重, 备注, 生产日期,  操作ip, 操作人员, 操作时间 ) ";
            //sql += " values(newid(),'" + 工厂编号 + "','" + 生产批号 + "',";
            //sql += " '" + 品名 + "','" + 钢号 + "','" + 生产批号 + "','" + 长度 + "','" + 规格 + "','" + 等级 + "','" + 表面状态 + "',";
            //sql += " '" + 表面处理 + "','" + 净重 + "','" + 标准 + "','" + 生产日期 + "','" + pub.getMac() + "','" + pub.sys_user.UserTrueName + "',getdate())";
            //pub.ExecuteSql(pub.ConnectionString, sql, "临时大标签打印塞入");
            //ba.Quit();

            string sql = "update  Bus_LablePrint set 是否已打印 = '是',打印次数 = ISNULL(打印次数,0)+1 where 生产批号 ='" + 生产批号 + "'";
            int r = pub.ExecuteSql(pub.ConnectionString, sql, "大标签打印次数更新");
            return r;
        }

        private int insertIntoBus_Volume(string 工厂编号, string 生产批号, string 存货编号, string departCode, string 品名, string 钢号, string 长度, string 厚度, string 规格, string 等级, string 表面状态, string 表面处理, string 净重, string 备注, string 生产日期, string 产地, string 仓库, string 锌层, string 包装形式, string 来源产线)
        {
            //先检查有没有
            string sqlquery = @"select VolumeCodeNew from Bus_Volume where VolumeCodeNew = '" + 生产批号 + "'";
            DataTable dt = pub.GetDataTable(pub.ConnectionString, sqlquery);
            if (dt != null && dt.Rows.Count > 0)
            {
                return 0;
            }
            else
            {
                string sql = @"insert into Bus_Volume ( guid,[WorkShopCode],[VolumeCodeNew],[FinishSize] ,[InventoryCode],[Material],[Address],[IsInStorage],StorageLocation,[WarehouseCode],[ZinCeng],PackageType)";
                sql += "values";
                sql += "( '" + Guid.NewGuid().ToString() + "','" + 工厂编号 + "','" + 生产批号 + "','" + 规格 + "' ,'" + 存货编号 + "','" + 钢号 + "','" + 产地 + "','在库','','" + 仓库 + "','" + 锌层 + "','" + 包装形式 + "')";
                return pub.ExecuteSql(pub.ConnectionString, sql, "保存成品入库状态单据");
            }
        }

        private string createBus_StockInV(string 工厂编号, string 生产批号, string 存货编号, string departCode, string 品名, string 钢号, string 长度, string 厚度, string 规格, string 等级, string 表面状态, string 表面处理, string 净重, string 备注, string 生产日期, string 产地, string 锌层, string 包装形式, string Customer, string 仓库, string 来源产线, string 颜色)
        {
            string guid = Guid.NewGuid().ToString();
            string Bguid = Guid.NewGuid().ToString();
            string docCode = pub.GetDocCode("产品入库", 工厂编号);
            decimal.TryParse(厚度, out decimal a);
            string sql;
            sql = " insert into Bus_StockInV(guid, StockInType, WorkShopCode, DocCode,  DepartCode, CreateID, Creator, CreateDate,IsSendToNC)";
            sql += " values('" + guid + "','产品入库 ','" + 工厂编号 + "','" + docCode + "', '" + departCode + "','" + pub.sys_user.User_Id + "','" + pub.sys_user.UserTrueName + "', getdate(),'N')";

            sql += @" insert into Bus_StockInVolume
                        (guid,Bguid, DocCode, RowNumber, WorkShopCode, WarehouseCode, StorageLocation, VolumeCodeNew, InventoryCode, Size, 
                        Thick,  Material,CID,Virtual,FaceStatus,FaceDone, Weight, Address,ProductDate,Grade,Meter,[ZinCeng],PackageType,Customer,WorkLine, PingMing,Color ) values ('" +
                        guid + "','" + Bguid + "', '" + docCode + "', '1', '" + 工厂编号 + "', '" + 仓库 + "', '', '" + 生产批号 + "', '" + 存货编号 + "', '" + 规格 + "', '"
                        + a + "',  '" + 钢号 + "','','C','" + 表面状态 + "',' " + 表面处理 + "','" + 净重 + "', '" + 产地 + "','" + 生产日期 + "','" + 等级 + "','" + 长度 + "',";
            sql += "'" + 锌层 + "','" + 包装形式 + "','" + Customer + "','" + 来源产线 + "','" + 品名 + "','" + 颜色 + "')";
            int r = pub.ExecuteSql(pub.ConnectionString, sql, "保存成品入库单据");
            string sqlqueryHeader = "select * from Bus_StockInV where guid='" + guid + "' ";
            DataTable tbr = pub.GetDataTable(pub.ConnectionString, sqlqueryHeader);
            if (tbr.Rows.Count > 0)
            {
                string sqlqueryBody = "select * from Bus_StockInVolume where guid='" + guid + "'";
                DataTable tbb = pub.GetDataTable(pub.ConnectionString, sqlqueryBody);
                if (tbb.Rows.Count > 0)
                {
                    MessageBox.Show("保存成功单号【" + docCode + "】!", "提示", MessageBoxButtons.OK);

                    return docCode;
                }
                else
                {
                    sql = "delete  Bus_StockInV  where guid='" + guid + "'";
                    pub.ExecuteSql(pub.ConnectionString, sql, "因为保存失败了，删除原料入库单");
                    MessageBox.Show("保存失败,代码【err1】!", "提示", MessageBoxButtons.OK);
                    return docCode;
                }
            }
            else
            {
                sql = "delete  Bus_StockInVolume  where guid='" + guid + "'";

                pub.ExecuteSql(pub.ConnectionString, sql, "因为保存失败了，删除原料入库单");
                MessageBox.Show("保存失败,代码【err2】!", "提示", MessageBoxButtons.OK);
                return docCode;
            }

        }
        private void sendToNC(string docCode)
        {
            string sql;
            sql = @"SELECT guid, StockInType, WorkShopCode, DocCode, DepartCode, SupplierCode, SupplierName, CreateID, Creator, 
                   CreateDate, ModifyID, Modifier, ModifyDate, ApproverID, Approver, ApproverDate, IsSendToNC, SendToNCDate
                   FROM      Bus_StockInV where docCode='" + docCode + "'";
            DataTable dtHeader = pub.GetDataTable(pub.ConnectionString, sql);

            if (dtHeader != null && dtHeader.Rows.Count > 0)
            {
                string sqlbody = @"SELECT [guid], [Bguid], [DocCode], [RowNumber], [WorkShopCode], [WarehouseCode], [StorageLocation], [T_CONTRACT_CODE],
                                [T_ORDER], [T_GOOD_UNIT], [T_GOOD_NUM], [T_GOOD_PRICE], [T_GOOD_MONEY], [VolumeCode], [VolumeCodeNew], [InventoryCode], 
                                [Size], [Thick], [Width], [Material], [Address], [Weight], [CID], [Virtual], [FaceStatus], [FaceDone], [NetWeight],
                                [Remark], [ProductDate],Grade
                                FROM      Bus_StockInVolume 
                                where 1=1 and DocCode='" + docCode + "' order by RowNumber  ";

                DataTable dtBody = pub.GetDataTable(pub.ConnectionString, sqlbody);
                if (dtBody != null && dtBody.Rows.Count > 0)
                {
                    NCModel.FinishedProductsInBillModel setInboundModel = new NCModel.FinishedProductsInBillModel();
                    //setInboundModel.corpName = "";
                    setInboundModel.region = "华中";
                    setInboundModel.ceateDate = DateTime.Parse(dtHeader.Rows[0]["CreateDate"].ToString()).ToString("yyyy-MM-dd");
                    //查询公司名称 公司编号
                    string sqlworkshop = @"SELECT  [WorkShopName]  FROM [Dic_WorkShop] where WorkShopCode = '" + dtHeader.Rows[0]["WorkShopCode"].ToString() + "'";
                    DataTable dtWorkShop = pub.GetDataTable(pub.ConnectionString, sqlworkshop);
                    if (dtWorkShop != null && dtWorkShop.Rows.Count > 0)
                    {
                        setInboundModel.corpName = dtWorkShop.Rows[0]["WorkShopName"].ToString();
                    }
                    else
                    {
                        MessageBox.Show("未能根据当前入库单公司编号查到对应的公司名称");
                        return;
                    }
                    setInboundModel.cWarehouseName = dtHeader.Rows[0]["WarehouseCode"].ToString();

                    List<NCModel.Item> itemList = new List<NCModel.Item>();
                    for (int i = 0; i < dtBody.Rows.Count; i++)
                    {
                        NCModel.Item item = new NCModel.Item();
                        //明细ID
                        item.rowNo = dtBody.Rows[0]["Bguid"].ToString();
                        //存货编码
                        item.cinvbascode = dtBody.Rows[0]["InventoryCode"].ToString();
                        //批次号
                        item.vbatchcode = dtBody.Rows[0]["VolumeCodeNew"].ToString(); ;
                        //入库数量
                        item.ninnum = "1";
                        //入库日期
                        item.dbilldate = DateTime.Parse(dtHeader.Rows[0]["CreateDate"].ToString()).ToString("yyyy-MM-dd"); ;
                        //颜色
                        item.vfree1 = dtBody.Rows[0]["FaceStatus"].ToString();
                        //长度
                        //item.vfree2 = dtBody.Rows[0]["FaceStatus"].ToString();
                        //产地
                        item.vfree3 = dtBody.Rows[0]["Address"].ToString();
                        //表面处理
                        item.vfree4 = dtBody.Rows[0]["FaceDone"].ToString();
                        //表面厚度
                        item.vfree5 = dtBody.Rows[0]["Thick"].ToString();
                        //等级
                        item.vuserdef8 = dtBody.Rows[0]["Grade"].ToString();
                        itemList.Add(item);
                    }


                    setInboundModel.items = itemList;

                    string jsonStr = JsonConvert.SerializeObject(setInboundModel);

                    //XmlDocument xml  = (XmlDocument)JsonConvert.DeserializeXmlNode(jsonStr,"root");
                    ExternalSystemToNcItfPortTypeClient client = new ExternalSystemToNcItfPortTypeClient();
                    string responseStr = client.setInbound(jsonStr);
                    NCModel.NCResponseModel nCResponse = JsonConvert.DeserializeObject<NCModel.NCResponseModel>(responseStr);
                    //调用成功，改写NC状态
                    if (nCResponse.state == 0)
                    {
                        string sqlUpdate = @"update Bus_StockInV set IsSendToNC = 'Y' ,SendToNCDate = '" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' where docCode = '" + docCode + "'";
                        int r = pub.ExecuteSql(pub.ConnectionString, sqlUpdate, "原料卷入库发送到NC");
                        if (r < 1)
                        {
                            MessageBox.Show("更新是否成功发送到NC失败");
                        }
                    }
                    MessageBox.Show(nCResponse.msg);

                }
                else
                {
                    MessageBox.Show("没查到行信息");
                }
            }
        }
        //private void sendToNC(string 单据号,string CreateDate, string Bguid, string 工厂编号, string 生产批号, string 存货编号, 
        //    string departCode, string 品名, string 钢号, string 长度, string 厚度, string 规格, string 等级, string 表面状态, string 表面处理,
        //    string 净重, string 备注, string 生产日期, string 产地)
        //{
        //    NCModel.SetInboundModel setInboundModel = new NCModel.SetInboundModel();
        //    setInboundModel.corpName = "";
        //    setInboundModel.region = "华中";
        //    setInboundModel.ceateDate = DateTime.Parse(CreateDate).ToString("yyyy-MM-dd");
        //    //仓库 地区 供应商先写死
        //    //setInboundModel.supplierName = "温州常展电气科技有限公司";
        //    //setInboundModel.supplierCode = "0577-WJ-0207";
        //    //查询公司名称 公司编号
        //    string sqlworkshop = @"SELECT  [WorkShopName]  FROM [Dic_WorkShop] where WorkShopCode = '" + 工厂编号 + "'";
        //    DataTable dtWorkShop = pub.GetDataTable(pub.ConnectionString, sqlworkshop);
        //    if (dtWorkShop != null && dtWorkShop.Rows.Count > 0)
        //    {
        //        setInboundModel.cWarehouseName = dtWorkShop.Rows[0]["WorkShopName"].ToString();
        //    }
        //    else
        //    {
        //        MessageBox.Show("未能根据当前入库单公司编号查到对应的公司名称");
        //        return;
        //    }

        //    List<NCModel.Item> itemList = new List<NCModel.Item>();

        //        NCModel.Item item = new NCModel.Item();
        //    //明细ID
        //        item.rowNo = Bguid;
        //    //存货编码
        //        item.cinvbascode = 存货编号;
        //    //批次号
        //    item.vbatchcode = 生产批号;
        //    //入库数量
        //    item.ninnum = "1";
        //    //入库日期
        //    item.dbilldate = DateTime.Parse(CreateDate).ToString("yyyy-MM-dd");
        //    //颜色
        //    item.vfree1 = 表面状态;
        //    //长度
        //    item.vfree2 = 长度;
        //    //产地
        //    item.vfree3 = 产地;
        //    //表面处理
        //    item.vfree4 = 表面处理;
        //    //表面厚度
        //    item.vfree5 = 厚度;
        //    //等级
        //    item.vuserdef8 = 等级;

        //    itemList.Add(item);

        //    setInboundModel.items = itemList;

        //    string jsonStr = JsonConvert.SerializeObject(setInboundModel);
        //    //XmlDocument xml  = (XmlDocument)JsonConvert.DeserializeXmlNode(jsonStr,"root");
        //    WMS2NC.ExternalSystemToNcItfPortTypeClient client = new WMS2NC.ExternalSystemToNcItfPortTypeClient();
        //    string responseStr = client.setInbound(jsonStr);
        //    NCModel.NCResponseModel nCResponse = JsonConvert.DeserializeObject<NCModel.NCResponseModel>(responseStr);
        //    //调用成功，改写NC状态
        //    if (nCResponse.state == 0)
        //    {
        //        string sqlUpdate = @"update Bus_StockInV set IsSendToNC = 'Y' ,SendToNCDate = '"
        //                            + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' where docCode = '" + docCode + "'";
        //        int r = pub.ExecuteSql(pub.ConnectionString, sqlUpdate, "原料卷入库发送到NC");
        //        if (r < 1)
        //        {
        //            MessageBox.Show("更新是否成功发送到NC失败");
        //        }
        //    }
        //}

        private string queryDeptCode(string tableName, string OutputVolumeNew)
        {
            DataTable dt = new DataTable();
            //string sql = @"select [OutputGuid], [guid], [Row], [VolumeCodeNew], [OutputVolumeNew], [OutputSize], [OutputThick], [OutputWidth], [OutputMaterial], [OutputWeight],
            //                [OutputMeter], [OutputQualityGrade], [OutputQualityInformation], [OutputEndStatus], [OutputNext], [OutputAddress], [PrintTimes], [DepartCode], [DepartName],
            //                [WorkLineCode], [WorkLineName], [WorkTeamCode], [WorkTeamName], [DayShiftCode], [DayShiftName], [CreateID], [Creator], [CreateDate], [ModifyID], [Modifier],
            //                [ModifyDate] where OutputVolumeNew = '" + OutputVolumeNew + "'";
            string sql = @"select   [DepartCode], [DepartName]
                            from " + tableName + " where OutputVolumeNew = '" + OutputVolumeNew + "'";
            dt = pub.GetDataTable(pub.ConnectionString, sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                return dt.Rows[0]["DepartCode"].ToString();
            }
            else
            {
                return "";

            }

        }

        private void uiButton匹配NC存货编号_Click(object sender, EventArgs e)
        {

            DataTable dt = uiDataGridView产出.DataSource as DataTable;
            if (dt != null && dt.Rows.Count > 0)
            {
                List<更新存货编号结果> 更新存货编号结果List = new List<更新存货编号结果>();
                for (int i = 0; i < dt.Rows.Count; i++)
                {

                    string 品名 = dt.Rows[i]["品名"].ToString();
                    string 规格 = dt.Rows[i]["规格"].ToString();
                    string 材质 = dt.Rows[i]["钢号"].ToString();
                    string 生产批号 = dt.Rows[i]["生产批号"].ToString();
                    string invCoe = pub.GetInventoryCode(品名, 规格, 材质);
                    更新存货编号结果 更新结果 = new 更新存货编号结果();
                    更新结果.生产批号 = 生产批号;
                    更新结果.品名 = 品名;
                    更新结果.规格 = 规格;
                    更新结果.材质 = 材质;
                    更新结果.存货编号 = invCoe;
                    if (!string.IsNullOrEmpty(invCoe))
                    {

                        string sqlUpdate = " update Bus_LablePrint set 存货编号 = '" + invCoe + "' where 生产批号 = '" + 生产批号 + "'";
                        int r = pub.ExecuteSql(pub.ConnectionString, sqlUpdate);
                        string sqlupdateStockinVol = "update Bus_StockInVolume set InventoryCode = '"+ invCoe + "' where VolumeCodeNew = '" + 生产批号 + "'";
                        int r2 = pub.ExecuteSql(pub.ConnectionString, sqlupdateStockinVol);
                        if (r <= 0)
                        {

                            更新结果.导入结果 = "更新失败";

                        }
                        else
                        {
                            更新结果.导入结果 = "更新成功";
                        }
                    }
                    更新存货编号结果List.Add(更新结果);
                }
                //弹出更新结果
                FormMsg formMsg = new FormMsg();

                listData();
            }
            //if (this.uiDataGridView产出.Rows.Count >0)
            //{
            //    for (int i=0;i<this.uiDataGridView产出.Rows.Count;i++)
            //    {
            //        string 品名 = this.uiDataGridView产出.Rows[i].Cells["C品名"].Value.ToString();
            //        string 规格 = this.uiDataGridView产出.Rows[i].Cells["C规格"].Value.ToString();
            //        string 材质= this.uiDataGridView产出.Rows[i].Cells["C钢号"].Value.ToString();
            //        string 卷号 = this.uiDataGridView产出.Rows[i].Cells["C生产批号"].Value.ToString();

            //    }
            //}
        }

        private void uiDataGridView产出_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            //if (e.RowIndex > -1 && uiDataGridView产出.Columns[e.ColumnIndex].Name == "C客户")
            //{
            //    DataTable dt = uiDataGridView产出.DataSource as DataTable;
            //    string 客户 = dt.Rows[e.RowIndex]["客户"].ToString();
            //    string 生产批号 = dt.Rows[e.RowIndex]["生产批号"].ToString();
            //    string sqlupdate = "update Bus_LablePrint set 客户 = '" + 客户 + "' where 生产批号 = '" + 生产批号 + "'";
            //    int r = pub.ExecuteSql(pub.ConnectionString, sqlupdate, "");
            //}
            if (e.RowIndex > -1 && uiDataGridView产出.Columns[e.ColumnIndex].Name == "C存货编号")
            {
                DataTable dt = uiDataGridView产出.DataSource as DataTable;
                string 存货编号 = dt.Rows[e.RowIndex]["存货编号"].ToString();
                string 生产批号 = dt.Rows[e.RowIndex]["生产批号"].ToString();
                string sqlupdate = "update Bus_LablePrint set 存货编号 = '" + 存货编号 + "' where 生产批号 = '" + 生产批号 + "'";
                int r = pub.ExecuteSql(pub.ConnectionString, sqlupdate, "");
            }
            //if (e.RowIndex > -1 && uiDataGridView产出.Columns[e.ColumnIndex].Name == "C钢号")
            //{
            //    DataTable dt = uiDataGridView产出.DataSource as DataTable;
            //    string 钢号 = dt.Rows[e.RowIndex]["客户"].ToString();
            //    string 生产批号 = dt.Rows[e.RowIndex]["生产批号"].ToString();
            //    string sqlupdate = "update Bus_LablePrint set 钢号 = '" + 钢号 + "' where 生产批号 = '" + 生产批号 + "'";
            //    int r = pub.ExecuteSql(pub.ConnectionString, sqlupdate, "");
            //}
        }

        private void checkBox是否已打印_CheckedChanged(object sender, EventArgs e)
        {
            listData();
        }

        private void Form打印标签_FormClosed(object sender, FormClosedEventArgs e)
        {
            ba.Quit();
        }

        private void uiButton批量打印_Click(object sender, EventArgs e)
        {
            uiButton批量打印.Enabled = false;
            try
            {
                //遍历行
                if (uiDataGridView产出 != null && uiDataGridView产出.Rows.Count > 0)
                {
                    for (int i = 0; i < uiDataGridView产出.Rows.Count; i++)
                    {
                        if (this.uiDataGridView产出.Rows[i].Cells["C选择"].Value != null)//打钩
                        {
                            if ((bool)this.uiDataGridView产出.Rows[i].Cells["C选择"].Value)
                            {

                                ////是否已经打印了
                                if (this.uiDataGridView产出.Rows[i].Cells["C是否已打印"].Value.ToString() == "是")
                                {
                                    if (MessageBox.Show("批号【" + uiDataGridView产出.Rows[i].Cells["C生产批号"].Value.ToString() + "】已经打印【" + uiDataGridView产出.Rows[i].Cells["C打印次数"].Value.ToString() + "】次，是否重新打印？"
                                        , "提示"
                                        , MessageBoxButtons.OKCancel) == DialogResult.OK)
                                    {
                                        打印(i);
                                    }
                                    else
                                    {

                                    }
                                }
                                else
                                {
                                    打印(i);
                                }
                            }

                        }
                    }
                }
            }
            catch (Exception)
            {
            }
            finally
            {
                uiButton批量打印.Enabled = true;
            }

        }

        private void 打印(int i)
        {
            DataTable dt = uiDataGridView产出.DataSource as DataTable;
            string 存货编号 = uiDataGridView产出.Rows[i].Cells["C存货编号"].Value.ToString();
            string 生产批号 = uiDataGridView产出.Rows[i].Cells["C生产批号"].Value.ToString();
            string 来源种类 = uiDataGridView产出.Rows[i].Cells["C来源种类"].Value.ToString();
            string Customer = uiDataGridView产出.Rows[i].Cells["C客户"].Value.ToString();
            string 品名 = uiDataGridView产出.Rows[i].Cells["C品名"].Value.ToString();
            string 钢号 = uiDataGridView产出.Rows[i].Cells["C钢号"].Value.ToString();
            string 规格 = uiDataGridView产出.Rows[i].Cells["C规格"].Value.ToString();
            if (uiDataGridView产出.Rows[i].Cells["C存货编号"] != null && !string.IsNullOrEmpty(uiDataGridView产出.Rows[i].Cells["C存货编号"].Value.ToString().Trim()))
            {

                string 工厂编号 = uiDataGridView产出.Rows[i].Cells["C工厂编号"].Value.ToString();
                string 来源产线 = uiDataGridView产出.Rows[i].Cells["C来源产线"].Value.ToString();
                //string 生产批号 = dt.Rows[e.RowIndex]["生产批号"].ToString();
                //string 品名 = dt.Rows[e.RowIndex]["品名"].ToString();
                //string 钢号 = dt.Rows[e.RowIndex]["钢号"].ToString();
                string 厚度 = uiDataGridView产出.Rows[i].Cells["C厚度"].Value.ToString();
                string 长度 = uiDataGridView产出.Rows[i].Cells["C长度"].Value.ToString();
                string 颜色 = uiDataGridView产出.Rows[i].Cells["C颜色"].Value.ToString();
                //string 规格 = dt.Rows[e.RowIndex]["规格"].ToString();
                string 等级 = uiDataGridView产出.Rows[i].Cells["C等级"].Value.ToString();
                string 表面状态 = uiDataGridView产出.Rows[i].Cells["C表面状态"].Value.ToString();
                string 表面处理 = uiDataGridView产出.Rows[i].Cells["C表面处理"].Value.ToString();
                string 净重 = uiDataGridView产出.Rows[i].Cells["C净重"].Value.ToString();
                string 备注 = uiDataGridView产出.Rows[i].Cells["C备注"].Value.ToString();
                string 生产日期 = uiDataGridView产出.Rows[i].Cells["C生产日期"].Value.ToString();
                string departCode = queryDeptCode(当前来源选项.Tag.ToString(), 生产批号);
                string 仓库 = uiDataGridView产出.Rows[i].Cells["C仓库"].Value.ToString();
                string 锌层 = 表面状态;
                string 包装形式 = uiDataGridView产出.Rows[i].Cells["C包装形式"].Value.ToString();
                string sql标准 = "select QualityStandard from [dbo].[Dic_QualityStandard] where  WorkTypeName = '" + 当前来源选项.Text.ToString() + "'";
                DataTable dt标准 = pub.GetDataTable(pub.ConnectionString, sql标准);
                string 标准 = dt标准?.Rows[0][0].ToString();
                //string 产地 = dt.Rows[e.RowIndex]["产地"].ToString();
                //产地暂时用万达
                string 产地 = uiDataGridView产出.Rows[i].Cells["C产地"].Value.ToString();
                //int r = print(来源种类, 工厂编号, 生产批号, 品名, 钢号, 长度, 规格, 等级, 表面状态, 表面处理, 净重, 标准, 生产日期);
                //塞入 Bus_Volume  
                int res = insertIntoBus_Volume(工厂编号, 生产批号, 存货编号, departCode, 品名, 钢号, 长度, 厚度, 规格, 等级, 表面状态, 表面处理, 净重, 备注, 生产日期, 产地, 仓库, 锌层, 包装形式, 来源产线);
                //生成入库单 检查这个批号有没有生成过
                string sqlqueryIsInstock = @"select VolumeCodeNew,IsSendToNC,Bus_StockInV.DocCode from Bus_StockInV
                                                left join Bus_StockInVolume on Bus_StockInV.guid = Bus_StockInVolume.guid
                                                where VolumeCodeNew =  '" + 生产批号 + "'";
                DataTable dtIsInstock = pub.GetDataTable(pub.ConnectionString, sqlqueryIsInstock);
                if (dtIsInstock != null && dtIsInstock.Rows.Count > 0)
                {
                    int r = print(来源种类, 工厂编号, 生产批号, 品名, 钢号, 长度, 规格, 等级, 表面状态, 表面处理, 净重, 标准, 生产日期);
                    if (dtIsInstock.Rows[0]["IsSendToNC"].ToString() == "N")
                    {
                        pub.sendToNC产成品(dtIsInstock.Rows[0]["DocCode"].ToString());
                    }
                }
                else
                {
                    int r = print(来源种类, 工厂编号, 生产批号, 品名, 钢号, 长度, 规格, 等级, 表面状态, 表面处理, 净重, 标准, 生产日期);
                    string docCode = createBus_StockInV(工厂编号, 生产批号, 存货编号, departCode, 品名, 钢号, 长度, 厚度, 规格, 等级, 表面状态, 表面处理, 净重, 备注, 生产日期, 产地, 锌层, 包装形式, Customer, 仓库, 来源产线, 颜色);
                    pub.sendToNC产成品(docCode);
                }
            }
            else
            {
                Form补录存货编码 form补录存货编码 = new Form补录存货编码();
                form补录存货编码.shortName = 品名;
                form补录存货编码.INVSPEC = 规格;
                form补录存货编码.INVTYPE = 钢号;
                if (form补录存货编码.ShowDialog(this) == DialogResult.OK)
                {
                    if (!string.IsNullOrEmpty(form补录存货编码.存货编号))
                    {
                        string 存货号 = form补录存货编码.存货编号;
                        //更新编码
                        string sqlupdateLabelPrintInvCode = "update Bus_LablePrint set 存货编号 = '" + 存货号 + "' where 生产批号 = '" + 生产批号 + "'";
                        pub.ExecuteSql(pub.ConnectionString, sqlupdateLabelPrintInvCode, "更新Bus_LablePrint表 生产批号：【" + 生产批号 + "】的存货编号【" + 存货编号 + "】");
                        update产出InvCode(来源种类, 生产批号, 存货号);
                        listData();
                    }
                    else
                    {
                        MessageBox.Show("缺少存货编号，不能打印");
                    }

                }
                else
                {
                    MessageBox.Show("缺少存货编号，不能打印");
                }

            }
        }

        private void uiDataGridView产出_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            开始行 = e.RowIndex;
            this.uiLabel开始.Text = 开始行.ToString();

            开始列 = e.ColumnIndex;
        }

        private void uiDataGridView产出_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            decimal 总重量 = 0;
            decimal 总米数 = 0;
            decimal 卷数 = 0;
            结束行 = e.RowIndex;
            结束列 = e.ColumnIndex;
            this.uiLabel结束.Text = 结束行.ToString();
            if (开始行 >= 0 && 结束行 > 0)
            {
                if (开始行 > 结束行)
                {
                    int k = 开始行;
                    开始行 = 结束行;
                    结束行 = k;
                }
                for (int i = 开始行; i <= 结束行; i++)
                {
                    卷数++;
                    总重量 += pub.Todecimal(this.uiDataGridView产出.Rows[i].Cells["C净重"].Value.ToString());
                    总米数 += pub.Todecimal(this.uiDataGridView产出.Rows[i].Cells["C长度"].Value.ToString());
                }

                this.uiTextBox总重量.Text = 总重量.ToString();
                this.uiTextBox总米数.Text = 总米数.ToString();
                this.uiTextBox总卷数.Text = 卷数.ToString();
            }
        }

        private void uiButton修改_Click(object sender, EventArgs e)
        {
            string 生产批号;
            string sql;
            string 结果值;

            结果值 = this.uiTextBox修改.Text.Trim().Replace("--", "");

            if (开始列 == 结束列)
            {
                if (开始行 >= 0)
                {
                    if (结束行 >= 0)
                    {
                        if (开始行 > 结束行)
                        {
                            int k = 开始行;
                            开始行 = 结束行;
                            结束行 = k;
                        }


                        if (this.uiDataGridView产出.Columns[开始列].Name == "C品名" && pub.sys_user.UserTrueName == "冯永婷")
                        {

                            if (结果值 == "")
                            {

                            }
                            else
                            {

                                if (MessageBox.Show("您要批改所选择的品名么?", "", MessageBoxButtons.OKCancel) == DialogResult.OK)
                                {
                                    for (int i = 开始行; i <= 结束行; i++)
                                    {
                                        生产批号 = this.uiDataGridView产出.Rows[i].Cells["C生产批号"].Value.ToString();
                                        sql = "select VolumeCodeNew from Bus_StockInVolume  ";
                                        sql += " where  VolumeCodeNew ='" + 生产批号 + "' ";
                                        DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
                                        if (tb.Rows.Count > 0)
                                        {

                                        }
                                        else
                                        {
                                            sql = "update  [Bus_LablePrint] set 品名='" + 结果值 + "'  where 生产批号='" + 生产批号 + "' ";
                                            int r = pub.ExecuteSql(pub.ConnectionString, sql, "打印大标签批量修改品名");
                                            this.uiDataGridView产出.Rows[i].Cells["C品名"].Value = 结果值;
                                        }


                                    }
                                }
                            }

                        }
                        if (this.uiDataGridView产出.Columns[开始列].Name == "C钢号" && pub.sys_user.UserTrueName == "冯永婷")
                        {
                            if (结果值 == "")
                            {

                            }
                            else
                            {    //检查未塞入用友
                                if (MessageBox.Show("您要批改所选择的钢号值么?", "", MessageBoxButtons.OKCancel) == DialogResult.OK)
                                {
                                    for (int i = 开始行; i <= 结束行; i++)
                                    {
                                        sql = "update  [Bus_LablePrint] set 钢号='" + 结果值 + "'  where 生产批号='" + this.uiDataGridView产出.Rows[i].Cells["C生产批号"].Value.ToString() + "' ";
                                      int r =  pub.ExecuteSql(pub.ConnectionString, sql, "");
                                        this.uiDataGridView产出.Rows[i].Cells["C钢号"].Value = 结果值;
                                    }
                                }
                            }

                        }
                        if (this.uiDataGridView产出.Columns[开始列].Name == "C规格" && pub.sys_user.UserTrueName == "冯永婷")
                        {
                            if (结果值 == "")
                            {

                            }
                            else
                            {    //检查未塞入用友
                                if (MessageBox.Show("您要批改所选择的规格值么?", "", MessageBoxButtons.OKCancel) == DialogResult.OK)
                                {
                                    for (int i = 开始行; i <= 结束行; i++)
                                    {
                                        sql = "update  [Bus_LablePrint] set 规格='" + 结果值 + "'  where 生产批号='" + this.uiDataGridView产出.Rows[i].Cells["C生产批号"].Value.ToString() + "' ";
                                        int r = pub.ExecuteSql(pub.ConnectionString, sql, "");
                                        this.uiDataGridView产出.Rows[i].Cells["C规格"].Value = 结果值;
                                    }
                                }
                            }
                        }
                        if (this.uiDataGridView产出.Columns[开始列].Name == "C净重" && pub.sys_user.UserTrueName == "冯永婷")
                        {
                            if (结果值 == "")
                            {

                            }
                            else
                            {    //检查未塞入用友
                                if (MessageBox.Show("您要批改所选择的净重值么?", "", MessageBoxButtons.OKCancel) == DialogResult.OK)
                                {
                                    for (int i = 开始行; i <= 结束行; i++)
                                    {
                                        sql = "update  [Bus_LablePrint] set 净重='" + 结果值 + "'  where 生产批号='" + this.uiDataGridView产出.Rows[i].Cells["C生产批号"].Value.ToString() + "' ";
                                        int r = pub.ExecuteSql(pub.ConnectionString, sql, "");
                                        this.uiDataGridView产出.Rows[i].Cells["C净重"].Value = 结果值;
                                    }
                                }
                            }
                        }
                        if (this.uiDataGridView产出.Columns[开始列].Name == "C长度" && pub.sys_user.UserTrueName == "冯永婷")
                        {
                            if (结果值 == "")
                            {

                            }
                            else
                            {    //检查未塞入用友
                                if (MessageBox.Show("您要批改所选择的长度值么?", "", MessageBoxButtons.OKCancel) == DialogResult.OK)
                                {
                                    for (int i = 开始行; i <= 结束行; i++)
                                    {
                                        sql = "update  [Bus_LablePrint] set 长度='" + 结果值 + "'  where 生产批号='" + this.uiDataGridView产出.Rows[i].Cells["C生产批号"].Value.ToString() + "' ";
                                        int r = pub.ExecuteSql(pub.ConnectionString, sql, "");
                                        this.uiDataGridView产出.Rows[i].Cells["C长度"].Value = 结果值;
                                    }
                                }
                            }
                        }
                        if (this.uiDataGridView产出.Columns[开始列].Name == "C客户" && (pub.sys_user.UserTrueName == "伍海林" || pub.sys_user.UserTrueName == "冯永婷" || pub.sys_user.UserTrueName == "谭震"))
                        {
                            if (结果值 == "")
                            {

                            }
                            else
                            {    //检查未塞入用友
                                if (MessageBox.Show("您要批改所选择的客户值么?", "", MessageBoxButtons.OKCancel) == DialogResult.OK)
                                {
                                    for (int i = 开始行; i <= 结束行; i++)
                                    {
                                        sql = "update  [Bus_LablePrint] set 客户='" + 结果值 + "'  where 生产批号='" + this.uiDataGridView产出.Rows[i].Cells["C生产批号"].Value.ToString() + "' ";
                                        int r = pub.ExecuteSql(pub.ConnectionString, sql, "");
                                        this.uiDataGridView产出.Rows[i].Cells["C客户"].Value = 结果值;
                                    }
                                }
                            }
                        }

                    }

                }
            }

        }
    }
}
